package com.ymatou.query.seller.trading.api.testcase;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.query.seller.trading.api.parameter.AmountInfo;
import com.ymatou.query.seller.trading.api.parameter.GetSellerSalesInfoArguments;
import com.ymatou.query.seller.trading.api.parameter.GetSellerSalesInfoBean;
import com.ymatou.query.seller.trading.api.parameter.GetSellerSalesInfoResponse;
import com.ymatou.query.seller.trading.api.parameter.GetSellerSalesInfoResult;
import com.ymatou.query.seller.trading.api.parameter.PeriodAmountSummary;
import com.ymatou.query.seller.trading.api.parameter.Sp_GetSellerSalesInfo_v2;
import com.ymatou.query.seller.trading.api.service.GetSellerSalesInfoV2Call;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;

/**
 * @date 2016-09-08
 * @author songyefeng
 * @desc 获取卖家销售额、成交额信息
 */
@SuppressWarnings("rawtypes")
@TestSuite
public class Ts_GetSellerSalesInfoV2 {

	private static GetSellerSalesInfoBean getSellerSalesInfoBean;
	private static GetSellerSalesInfoV2Call getSellerSalesInfoV2Call;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取卖家销售额、成交额信息");
	}

	@Before
	public void caseUp() {
		getSellerSalesInfoBean = new GetSellerSalesInfoBean();
		getSellerSalesInfoV2Call = new GetSellerSalesInfoV2Call();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getSellerSalesInfoV2Call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetSellerSalesInfoResponse expectedResponse = new GetSellerSalesInfoResponse();
		ConstructExpectedResponse(expectedResponse);
		GetSellerSalesInfoResponse actualResponse = getSellerSalesInfoV2Call.getSellerSalesInfoResponse();
		
		Logger.comment("***********************开始验证TodaySales节点***********************");
		AmountInfo expTodaySales = expectedResponse.getTodaySales();
		AmountInfo actTodaySales = actualResponse.getTodaySales();
		DataUtil.verifyDecimalEquals(expTodaySales.getSellerCoupon(), actTodaySales.getSellerCoupon(), "验证TodaySales.SellerCoupon");
		DataUtil.verifyDecimalEquals(expTodaySales.getShangou(), actTodaySales.getShangou(), "验证TodaySales.Shangou");
		DataUtil.verifyDecimalEquals(expTodaySales.getSpot(), actTodaySales.getSpot(), "验证TodaySales.Spot");
		Logger.comment("***********************完成验证TodaySales节点***********************");
		
		Logger.comment("***********************开始验证Sales节点***********************");
		PeriodAmountSummary expSales = expectedResponse.getSales();
		PeriodAmountSummary actSales = actualResponse.getSales();
		Logger.comment("开始验证Sales.Today节点");
		AmountInfo expSalesToday = expSales.getToday();
		AmountInfo actSalesToday = actSales.getToday();
		verifyAmountInfo(expSalesToday, actSalesToday);
		Logger.comment("完成验证Sales.Today节点");
		Logger.comment("开始验证Sales.Yesterday节点");
		AmountInfo expSalesYesterday = expSales.getYesterday();
		AmountInfo actSalesYesterday = expSales.getYesterday();
		verifyAmountInfo(expSalesYesterday, actSalesYesterday);
		Logger.comment("完成验证Sales.Yesterday节点");
		Logger.comment("开始验证Sales.Week节点");
		AmountInfo expSalesWeek = expSales.getWeek();
		AmountInfo actSalesWeek = expSales.getWeek();
		verifyAmountInfo(expSalesWeek, actSalesWeek);
		Logger.comment("完成验证Sales.Week节点");
		Logger.comment("开始验证Sales.Month节点");
		AmountInfo expSalesMonth = expSales.getMonth();
		AmountInfo actSalesMonth = expSales.getMonth();
		verifyAmountInfo(expSalesMonth, actSalesMonth);
		Logger.comment("完成验证Sales.Month节点");
		Logger.comment("***********************完成验证Sales节点***********************");
		
		Logger.comment("***********************开始验证Turnovers节点***********************");
		PeriodAmountSummary expTurnovers = expectedResponse.getTurnovers();
		PeriodAmountSummary actTurnovers = actualResponse.getTurnovers();
		Logger.comment("开始验证Turnovers.Today节点");
		AmountInfo expTurnoversToday = expTurnovers.getToday();
		AmountInfo actTurnoversToday = actTurnovers.getToday();
		verifyAmountInfo(expTurnoversToday, actTurnoversToday);
		Logger.comment("完成验证Turnovers.Today节点");
		Logger.comment("开始验证Turnovers.Yesterday节点");
		AmountInfo expTurnoversYesterday = expTurnovers.getYesterday();
		AmountInfo actTurnoversYesterday = actTurnovers.getYesterday();
		verifyAmountInfo(expTurnoversYesterday, actTurnoversYesterday);
		Logger.comment("完成验证Turnovers.Yesterday节点");
		Logger.comment("开始验证Turnovers.Week节点");
		AmountInfo expTurnoversWeek = expTurnovers.getWeek();
		AmountInfo actTurnoversWeek = actTurnovers.getWeek();
		verifyAmountInfo(expTurnoversWeek, actTurnoversWeek);
		Logger.comment("完成验证Turnovers.Week节点");
		Logger.comment("开始验证Turnovers.Month节点");
		AmountInfo expTurnoversMonth = expTurnovers.getMonth();
		AmountInfo actTurnoversMonth = actTurnovers.getMonth();
		verifyAmountInfo(expTurnoversMonth, actTurnoversMonth);
		Logger.comment("完成验证Turnovers.Month节点");
		Logger.comment("***********************完成验证Turnovers节点***********************");
	}
	
	private void verifyAmountInfo(AmountInfo expect,AmountInfo actual){
		DataUtil.verifyDecimalEquals(expect.getSellerCoupon(), actual.getSellerCoupon(), "验证SellerCoupon");
		DataUtil.verifyDecimalEquals(expect.getShangou(), actual.getShangou(), "验证Shangou");
		DataUtil.verifyDecimalEquals(expect.getSpot(), actual.getSpot(), "验证Spot");
	}

	private void ConstructExpectedResponse(GetSellerSalesInfoResponse response) {
		GetSellerSalesInfoArguments args = new GetSellerSalesInfoArguments();
		args.setSellerId(DataUtil.Integerr
				.GetValueOrDefault(getSellerSalesInfoBean.getSellerId()));
		
		GetSellerSalesInfoResult result = QueryMultiple(args);
		
		response.setSales(GetSummary(result.getSales()));
		response.setTodaySales(response.getSales().getToday());
		
		response.setTurnovers(GetSummary(result.getTurnovers()));
	}

	private AmountInfo GetInfo(GetSellerSalesInfoResult.SalesInfo info) {
		AmountInfo o = new AmountInfo();
		o.setSpot(info.getSpot());
		o.setShangou(info.getShangou());
		o.setSellerCoupon(info.getSellerCoupon());
		return o;
	}

	private PeriodAmountSummary GetSummary(
			GetSellerSalesInfoResult.SalesSummary o) {
		PeriodAmountSummary x = new PeriodAmountSummary();
		x.setMonth(GetInfo(o.getMonth()));
		x.setToday(GetInfo(o.getToday()));
		x.setWeek(GetInfo(o.getWeek()));
		x.setYesterday(GetInfo(o.getYesterday()));
		return x;
	}

	private GetSellerSalesInfoResult QueryMultiple(
			GetSellerSalesInfoArguments args) {
		Sp_GetSellerSalesInfo_v2 sp = new Sp_GetSellerSalesInfo_v2();
		sp.setSellerId(args.getSellerId());
		sp.setDate(args.getDate());
		sp.execute();
		List<Map> saleList = sp.getSaleList();
		List<Map> dealList = sp.getDealList();
		return Convert(saleList, dealList);
	}

	private GetSellerSalesInfoResult Convert(List<Map> saleList,
			List<Map> dealList) {
		GetSellerSalesInfoResult result = new GetSellerSalesInfoResult();
		GetSellerSalesInfoResult.SalesSummary sales = new GetSellerSalesInfoResult.SalesSummary();
		sales.setYesterday(mapToBean(saleList, "yesterday"));
		sales.setToday(mapToBean(saleList, "today"));
		sales.setWeek(mapToBean(saleList, "week"));
		sales.setMonth(mapToBean(saleList, "month"));
		result.setSales(sales);

		GetSellerSalesInfoResult.SalesSummary deals = new GetSellerSalesInfoResult.SalesSummary();
		deals.setYesterday(mapToBean(dealList, "yesterday"));
		deals.setToday(mapToBean(dealList, "today"));
		deals.setWeek(mapToBean(dealList, "week"));
		deals.setMonth(mapToBean(dealList, "month"));
		result.setTurnovers(deals);
		return result;
	}

	private GetSellerSalesInfoResult.SalesInfo mapToBean(List<Map> list,
			String day) {
		GetSellerSalesInfoResult.SalesInfo bean = new GetSellerSalesInfoResult.SalesInfo();
		Map map = list.stream().filter(x -> x.get("TimeSpan").equals(day))
				.collect(Collectors.toList()).get(0);
		bean.setTimeSpan(day);
		bean.setShangou(DataUtil.BigDecimall.GetValueOrDefault(map
				.get("Shangou")));
		bean.setSpot(DataUtil.BigDecimall.GetValueOrDefault(map.get("Spot")));
		bean.setSellerCoupon(DataUtil.BigDecimall.GetValueOrDefault(map
				.get("SellerCoupon")));
		return bean;
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerSalesInfoV2_001() {
		Logger.start(true, "获取卖家销售额、成交额信息成功");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			getSellerSalesInfoBean.setSellerId(sellerId);
			getSellerSalesInfoV2Call.setData(getSellerSalesInfoBean);
			getSellerSalesInfoV2Call.callService();
			Logger.comment("getSellerSalesInfoV2Call send:"
					+ getSellerSalesInfoV2Call.getEntityString());
			Logger.comment("getSellerSalesInfoV2Call response:"
					+ getSellerSalesInfoV2Call.getReturnData());
			Logger.verifyEquals("200", getSellerSalesInfoV2Call.getCode(),
					"验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}